Yes — looks like we wrote the same code twice.
super
in a Child's Method
Sometimes (as in the example) you want a child class to have its own
method, but that method includes everything the parent's method does.
You can use the super
reference in this situation.
For example,
here is Video's
method:
public void show() { System.out.println( title + ", " + length + " min. available:" + avail ); }
Here is Movie's
method without using super:
public void show() { System.out.println( title + ", " + length + " min. available:" + avail ); System.out.println( "dir: " + director + " " + rating ); }
Movie
's method would better be written using super:
public void show() { super.show(); System.out.println( "dir: " + director + " " + rating ); }
Unlike the case when super
is used in a constructor,
inside a method super
does not have to be used in the first statement.
Think of two reasons why using super
in this way is
a good thing to do.